package saxion.speelveld.spaceengineer;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.controller.simpleSpinnerDealAdapter;
import saxion.speelveld.spaceengineer.controller.tradePlanetsSimpleListAdapter;
import saxion.speelveld.spaceengineer.model.ResourceAndPlanetWrapper;
import saxion.speelveld.spaceengineer.model.ResourceType;
import saxion.speelveld.spaceengineer.model.ResourceWrapper;
import saxion.speelveld.spaceengineer.model.TradeDeal;
import saxion.speelveld.spaceengineer.model.TradePlanet;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class MarketScreen extends Activity
{
	private TextView currentMoney;

	private ListView planetList;

	private ImageView planetOrTradeImage;

	private TextView planetInformation;

	private TextView tradeOptions;

	private Spinner tradeDeals;

	private TextView tradeDealcost;

	private Button tradeTheDeal;

	private ListView currentTradePlanets;

	private int givenMoney = 0;

	TradePlanet[] tradePlanets;

	ArrayList<TradeDeal> tradeableDeals;

	private ResourceWrapper resourceEdits;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_market_screen);

		currentMoney = (TextView) findViewById(R.id.tv_current_money);
		planetList = (ListView) findViewById(R.id.lv_current_planets);
		planetOrTradeImage = (ImageView) findViewById(R.id.im_planet_image_or_trade_item_image);
		planetInformation = (TextView) findViewById(R.id.tv_planet_information);
		tradeOptions = (TextView) findViewById(R.id.tv_trade_options);
		tradeDeals = (Spinner) findViewById(R.id.spinner_trade_deals);
		tradeDealcost = (TextView) findViewById(R.id.tv_trade_deal_cost);
		tradeTheDeal = (Button) findViewById(R.id.button_trade_deal);

		ResourceAndPlanetWrapper wrapper = (ResourceAndPlanetWrapper) getIntent().getSerializableExtra(
			"MarketScreen.TradeWrapper");

		currentMoney.setText("Current money: " + wrapper.getMoney() + "");
		givenMoney = wrapper.getMoney();
		tradePlanets = wrapper.getTradePlanetArray();

		tradePlanetsSimpleListAdapter adapter = new tradePlanetsSimpleListAdapter(this.getApplicationContext(),
			tradePlanets);
		planetList.setAdapter(adapter);

		planetList.setOnItemClickListener(new MyItemOnClickListener());
		tradeTheDeal.setOnClickListener(new tradeClickListener());
		setSelected(0);
		tradeDeals.setOnItemSelectedListener(new MyOnItemSelectedListener());
	}

	private class MyItemOnClickListener implements OnItemClickListener
	{

		private TradePlanet tradePlanet;

		private ArrayList<TradeDeal> tradeDeals = new ArrayList<TradeDeal>();

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int posistion, long clickedItemId)
		{
			setSelected(posistion);
		}
	}

	private void setSelected(int posistion)
	{
		ArrayList<TradeDeal> tradeDeals = new ArrayList<TradeDeal>();
		// een individuele trade planet.
		TradePlanet tradePlanet = tradePlanets[posistion];

		// alle deals die in de spinner verwerkt moeten worden.
		tradeableDeals = tradePlanet.getDeals();

		// de planeet image
		planetOrTradeImage.setImageResource(tradePlanet.getPlanetImage());

		// de planeet naam
		planetInformation.setText(tradePlanet.getPlanetName());

		simpleSpinnerDealAdapter adapter = new simpleSpinnerDealAdapter(getApplicationContext(), tradeableDeals);
		MarketScreen.this.tradeDeals.setAdapter(adapter);

	}

	private class tradeClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			TradeDeal thedeal = (TradeDeal) MarketScreen.this.tradeDeals.getSelectedItem();
			if (resourceEdits == null)
			{
				resourceEdits = new ResourceWrapper();
			}
			if (givenMoney - thedeal.cost >= 0)
			{
				currentMoney.setText("Current money :" + (givenMoney - thedeal.cost) + "");
				resourceEdits.addResourceAmount(thedeal.tradeType, thedeal.amount);
				resourceEdits.addResourceAmount(ResourceType.MONEY, 0 - thedeal.cost);
				givenMoney -= thedeal.cost;
			}

		}
	}

	private class MyOnItemSelectedListener implements OnItemSelectedListener
	{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3)
		{
			tradeDealcost.setText(Integer.toString(tradeableDeals.get(pos).cost));
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
		{
			tradeDeals.setSelection(0);

		}

	}

	public void onBackPressed()
	{
		if (resourceEdits != null)
		{
			Log.d("Hai", "been here");
			Intent i = new Intent();
			i.putExtra("Resources", resourceEdits);
			setResult(RESULT_OK, i);
			finish();
		}
		else
		{
			setResult(RESULT_CANCELED);
			finish();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_market_screen, menu);
		return true;
	}

}
